package com.gzhuxj201.controller;

import com.gzhuxj201.entity.Course;
import com.gzhuxj201.entity.Result;
import com.gzhuxj201.entity.Student;
import com.gzhuxj201.entity.Teacher;
import com.gzhuxj201.service.CourseService;
import com.gzhuxj201.service.SCService;
import com.gzhuxj201.service.TCService;
import com.gzhuxj201.util.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private TCService tcService;
    @Autowired
    private SCService scService;


    @GetMapping("/CourseIsSelect")
    public Result selectAllForCourseSelection(){
        //查询course表获取可选课状态(canSelect=1)的课程信息
       return courseService.selectByCanSelect();
    }

    @DeleteMapping("{cno}")
    public Result deleteByCno(@PathVariable String cno) throws InterruptedException {
        int res=courseService.deleteByCno(cno);
        return Result.success("删除成功");
    }

    @PutMapping
    public Result updateByCno(@RequestBody Course course) throws InterruptedException {
       int res=courseService.updateByCno(course);
        return Result.success();
    }

    @GetMapping("{cno}")
    public Result updateByCno(@PathVariable String cno){
        Course course=courseService.selectByCno(cno);
        return Result.success(course);
    }

    @PostMapping
    public Result add(@RequestBody Course course) throws InterruptedException {
        //调用courseService方法添加course
       int res= courseService.add(course);
        return Result.success("添加成功");
    }

    @GetMapping("/forStu")
    public Result selectAllForStu(){
        //获取学生信息
        Student student = (Student) UserHolder.getUser();
        //根据cno查询sc表获取学生所选的课程号cno有哪些
        List<String> cnos= scService.selectBySno(student.getSno());
        //根据cno查询course表获取课程信息

        List<Course> courseList= courseService.selectByCnos(cnos);
        return Result.success(courseList);
    }

    @GetMapping
    public Result selectAll(){
       Teacher teacher = (Teacher) UserHolder.getUser();
        System.out.println("tl:tea:"+teacher);
        //根据tno查询tc表获取教师所教的课程号cno有哪些
       List<String> cnos= tcService.selectByTno(teacher.getTno());
        //根据cno查询course表获取课程信息

       List<Course> courseList= courseService.selectByCnos(cnos);
        return Result.success(courseList);
    }
}
